//
//  ViewController.m
//  9-18
//
//  Created by mac on 16/9/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *label2;
@property (retain, nonatomic)UILabel *label;
@property (retain,nonatomic) UIButton *buton;
@property (retain,nonatomic) NSMutableString *string;//暂时存放要输入的数字
@property (assign,nonatomic) double valu,s,father;//valu为输入的值，s为结果，father为0时不进行符号运算（为了防止不停的输入运算符而进行叠加运算）
@property (retain,nonatomic) NSString *str;//运算符
@property () NSInteger lenth;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    CGRect screens = [UIScreen mainScreen].bounds;
     NSLog(@"宽度：%f,高度：%f",screens.size.width,screens.size.height);
    
    self.view.backgroundColor = [UIColor blackColor];
    self.label2.text = @"";
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 96, 320, 76)];
    self.label.backgroundColor = [UIColor blackColor];
    self.label.textColor = [UIColor whiteColor];
    self.label.textAlignment =  NSTextAlignmentRight;
    self.label.font = [UIFont systemFontOfSize:32];
    self.label.text = @"0";
    [self.view addSubview:_label];
    
    NSArray *arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
    
    int n = 0;
    for(int i = 0; i<3 ; i++)//1-9
    {
        for(int j = 0 ; j<3 ; j++)
        {
            self.buton = [UIButton buttonWithType:UIButtonTypeSystem];
            
            self.buton.frame = CGRectMake(5 + 80 * j, 253 + 80 * i, 75, 75);
 
            [self.buton setBackgroundColor:[UIColor whiteColor]];
            
            [self.buton setTintColor:[UIColor blackColor]];
            
            [self.buton setTitle:arr[n++] forState:UIControlStateNormal];
            
            self.buton.titleLabel.font = [UIFont systemFontOfSize:18];
            
            [self.buton addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.view addSubview:self.buton];
        }
    }
    
    //0键
    UIButton *buton1 = [UIButton buttonWithType:UIButtonTypeSystem];
    buton1.frame = CGRectMake(5, 493, 155, 75);
    [buton1 setBackgroundColor:[UIColor whiteColor]];
    [buton1 setTintColor:[UIColor blackColor]];
    [buton1 setTitle:@"0" forState:UIControlStateNormal];
    [buton1 addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buton1];
    
    
    // + - * /
    NSArray *arr2 = [NSArray arrayWithObjects:@"+",@"-",@"*",@"/",nil];
    n=0;
    for(int i = 0 ; i<4 ; i++)
    {
        UIButton *operatorButton = [UIButton buttonWithType:UIButtonTypeSystem];
        operatorButton.frame = CGRectMake(245, 253 + 80 * i, 75, 75);
        [operatorButton setBackgroundColor:[UIColor orangeColor]];
        [operatorButton setTintColor:[UIColor whiteColor]];
        [operatorButton setTitle:arr2[n++] forState:UIControlStateNormal];
        operatorButton.titleLabel.font = [UIFont systemFontOfSize:30];
        
        [operatorButton addTarget:self action:@selector(operationNumber:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:operatorButton];
    }
    
    //等于号 ＝
    UIButton *equalButon = [UIButton buttonWithType:UIButtonTypeSystem];
    equalButon.frame = CGRectMake(245, 173, 75, 75);
    [equalButon setBackgroundColor:[UIColor orangeColor]];
    [equalButon setTintColor:[UIColor whiteColor]];
    [equalButon setTitle:@"=" forState:UIControlStateNormal];
    equalButon.titleLabel.font = [UIFont systemFontOfSize:30];
    
    [equalButon addTarget:self action:@selector(operationNumber:) forControlEvents: UIControlEventTouchUpInside];
    
    [self.view addSubview:equalButon];
    
    // 小数点
    UIButton *spotButon = [UIButton buttonWithType:UIButtonTypeSystem];
    spotButon.frame = CGRectMake(165, 493, 75, 75);
    [spotButon setBackgroundColor:[UIColor whiteColor]];
    [spotButon setTintColor:[UIColor blackColor]];
    [spotButon setTitle:@"." forState:UIControlStateNormal];
    spotButon.titleLabel.font = [UIFont systemFontOfSize:18];
    [spotButon addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:spotButon];
   
    //归零键
    UIButton *ACButon = [UIButton buttonWithType:UIButtonTypeSystem];
    ACButon.frame = CGRectMake(5, 173, 75, 75);
    [ACButon setBackgroundColor:[UIColor lightGrayColor]];
    [ACButon setTintColor:[UIColor blueColor]];
    [ACButon setTitle:@"AC" forState:UIControlStateNormal];
    ACButon.titleLabel.font = [UIFont systemFontOfSize:18];
    
    [ACButon addTarget:self action:@selector(clean) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ACButon];
    
    //清除键
    UIButton *deletButon = [UIButton buttonWithType:UIButtonTypeSystem];
    deletButon.frame = CGRectMake(85, 173, 155, 75);
    [deletButon setBackgroundColor:[UIColor lightGrayColor]];
    [deletButon setTintColor:[UIColor redColor]];
    [deletButon setTitle:@"DEL" forState:UIControlStateNormal];
    deletButon.titleLabel.font = [UIFont systemFontOfSize:18];
    [deletButon addTarget:self action:@selector(delette) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:deletButon];
    
    self.lenth = 10;
    //初始化这两个字符串
    _str = @"";
    self.string=[NSMutableString new];
   
}

- (void)number:(UIButton *)sender
{
    [self.string appendString:[sender currentTitle]];
    //获取按钮的titile，不是使用sender.titleLabel.text
    
    if(self.string.length<=_lenth){
        _father = 1;
        if(![_str isEqualToString:@""])
        {
            self.label2.text = _str;
        }
        self.label.text = _string;
        self.valu = [_string doubleValue];
        NSLog(@"valu:%g",_valu);
    }
    
}

- (void)operationNumber:(UIButton *)sender//判断运算符与运算方法
{
    
    if([_str isEqualToString:@""])
    {
        _str = [sender currentTitle];
        _s = _valu;
        _father = 0;
        self.label2.text = self.label.text;
        self.label.text = _str;
        
        [self.string setString:@""];//初始化，以方便下次输入数字

        
        if([_str isEqualToString:@"="]) {
            self.label2.text = @"=";
            self.label.text = [NSString stringWithFormat:@"%10.2lf",_s];
                _str = [NSString string];
        }
    }
    else
    {
            if([_str isEqualToString:@"+"])
        {
            _str = [sender currentTitle];
            if(![self.label.text isEqualToString:_str])self.label2.text = self.label.text;
            
            self.label.text = _str;
            if(_father !=0){
                _father = 0;
                _s = _s + _valu;
                self.label2.text = [NSString stringWithFormat:@"%10.2lf",_s];
                [self.string setString:@""];
            }
        }
       else if([_str isEqualToString:@"-"])
        {
            _str = [sender currentTitle];
            if(![self.label.text isEqualToString:_str])self.label2.text = self.label.text;

            self.label.text = _str;
            if(_father !=0){
                _father = 0;
                _s = _s - _valu;
                self.label2.text = [NSString stringWithFormat:@"%10.2lf",_s];
                [self.string setString:@""];
            }

        }
        else if([_str isEqualToString:@"*"])
        {
            _str = [sender currentTitle];
            if(![self.label.text isEqualToString:_str])self.label2.text = self.label.text;

            self.label.text = _str;
            if(_father !=0){
                _father = 0;
                _s = _s * _valu;
                self.label2.text = [NSString stringWithFormat:@"%10.2lf",_s];
                [self.string setString:@""];
            }
        }
        else if([_str isEqualToString:@"/"])
        {
            _str = [sender currentTitle];
            if(![self.label.text isEqualToString:_str])self.label2.text = self.label.text;

            self.label.text = _str;
            if(_father !=0){
                _father = 0;
                _s = _s / _valu;
                self.label2.text = [NSString stringWithFormat:@"%10.2lf",_s];
                [self.string setString:@""];
            }
        }
        if([[sender currentTitle] isEqualToString:@"="])
        {
            _str = [NSString string];
            [_string setString:@""];
                _valu = _s;
            self.label2.text = @"=";
             self.label.text = [NSString stringWithFormat:@"%10.2lf",_s];
            
        }

    }
    NSLog(@"结果：%g",_s);

}

- (void)clean//归零方法
{
    _str = [NSString string];
    [_string setString:@""];
    _valu = _s = _father = 0;
    self.label.text = @"0";
    self.label2.text = @"";
}

- (void)delette
{
    if(![_string isEqualToString:@""])
    {
        [_string deleteCharactersInRange:NSMakeRange(_string.length - 1, 1)];//NSMakeRange(位置，长度);
        _valu = [_string doubleValue];
        self.label.text = _string;
        
        if([_string isEqualToString:@""]) self.label.text = @"0";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
